package com.sbs.gaoxinqutoilet.base;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.sbs.gaoxinqutoilet.R;
import com.sbs.gaoxinqutoilet.entity.CheckToken;
import com.sbs.gaoxinqutoilet.entity.UserEntity;
import com.sbs.gaoxinqutoilet.http.HttpApis;
import com.sbs.gaoxinqutoilet.http.Request;
import com.sbs.gaoxinqutoilet.interfaces.Constant;
import com.sbs.gaoxinqutoilet.interfaces.Constants;
import com.sbs.gaoxinqutoilet.interfaces.DialogClick;
import com.sbs.gaoxinqutoilet.myapp.MyApplication;
import com.sbs.gaoxinqutoilet.utils.GsonTools;
import com.sbs.gaoxinqutoilet.utils.HttpUtils;
import com.sbs.gaoxinqutoilet.utils.JsonUtil;
import com.sbs.gaoxinqutoilet.utils.LogUtils;
import com.sbs.gaoxinqutoilet.utils.SPUtil;
import com.sbs.gaoxinqutoilet.utils.ToastUtils;
import com.sbs.gaoxinqutoilet.utils.loading.IosAlertDialog;

import java.util.HashMap;

import me.yokeyword.fragmentation.SupportFragment;

import static com.sbs.gaoxinqutoilet.utils.loading.LoadingDialog.cancelLoadingDialog;


/*
*
* 四个主Fragment 继承的 base
* */
public class BaseMainFragment extends SupportFragment {

    // 再点一次退出程序时间设置
    private static final long WAIT_TIME = 2000L;
    private long TOUCH_TIME = 0;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String token_data = SPUtil.getString(Constants.KEY_TOKEN, "");
        Log.e("token","token值:"+token_data);
        //check_token
        String url_check_token= HttpApis.check_token+token_data;
//        HashMap<String, String> map = new HashMap<>();
//        map.put("token", token_data);
        HttpUtils.requestGet(getContext(), url_check_token,  new Request(){
            @Override
            public void success(String jsonStr) {
                super.success(jsonStr);
                Log.e("jsonStr","jsonStr值:"+jsonStr);
                CheckToken checkToken = JsonUtil.json2Bean(jsonStr, CheckToken.class);
                if(checkToken==null||!"1".equals(checkToken.getState())){
                    //重新登录--
                    //访问重新登录接口
                    //姓名
                    String login_name = SPUtil.getString(Constants.KEY_USERNAME, "");
                    //登录密码
                    String login_pass=SPUtil.getString(Constant.KEY_USERPWD, "");

                    getReLogin(login_name,login_pass);

                }
            }

            @Override
            public void error(String error) {
                super.error(error);
                Log.e("jsonStr","error值:"+error);
            }
        });
    }

    private void getReLogin(String login_name, String login_pass) {
        String mBuyUrl = HttpApis.login;
        HashMap<String ,String> map=new HashMap<>();
        map.put("name", login_name);
        map.put("password", login_pass);

        HttpUtils.requestPost(_mActivity, mBuyUrl, map, new Request(){
            @Override
            public void success(String jsonStr) {
                super.success(jsonStr);
                LogUtils.i("loginAccount", "success: "+jsonStr);

                UserEntity loginEntity = GsonTools.changeGsonToBean(jsonStr, UserEntity.class);
                //数据解析
                if (loginEntity != null) {
                    //姓名
                    SPUtil.putString(Constants.KEY_USERNAME, login_name);
                    //不保存登录密码
                    SPUtil.putString(Constant.KEY_USERPWD, login_pass);
                    //login  token  每次请求都要带有这个token
                    SPUtil.putString(Constants.KEY_TOKEN, loginEntity.getToken());
                    LogUtils.i("show_token_data",loginEntity.getToken());
                    MyApplication.setUserEntity(loginEntity);
                    SPUtil.putBeanValue(Constants.KEY_USER, loginEntity);

                }
                cancelLoadingDialog();
            }

            @Override
            public void error(String error) {
                super.error(error);

            }
        });
    }

    /**
     * 处理回退事件
     *
     * @return
     */
    @Override
    public boolean onBackPressedSupport() {
        if (System.currentTimeMillis() - TOUCH_TIME < WAIT_TIME) {
            _mActivity.finish();
        } else {
            TOUCH_TIME = System.currentTimeMillis();
            Toast.makeText(_mActivity, R.string.press_again_exit, Toast.LENGTH_SHORT).show();
        }

        return true;
    }

    protected void showAlertDialogNoMsg(Context context, String titleContent, String message, final DialogClick dialogClick) {

        new IosAlertDialog(context).builder().setTitle(titleContent)
                .setPositiveButton("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialogClick.PositiveMessage();
                    }
                }).setNegativeButton("取消", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogClick.NegativeMessage();
            }
        }).show();
    }


    //Toast显示
    protected void showShortToast(Context context,String string) {
        ToastUtils.showShortToast(context, string);
    }

}
